//
//  PhotoMapView.m
//  XPhoto
//
//  Created by baboy on 2019/7/15.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "PhotoMapView.h"
#import "BKAnnotation.h"
#import "BKAnnotationView.h"

@implementation PhotoMapView

- (void)setItems:(NSMutableArray *)items{
    _items = [NSMutableArray arrayWithArray:items];
    MKMapRect visibleRect = MKMapRectNull;
    MKCoordinateSpan theSpan;
    //地图的范围 越小越精确
    theSpan.latitudeDelta = 0.01;
    theSpan.longitudeDelta = 0.01;
    MKCoordinateRegion visibleRegion;
    visibleRegion.span = theSpan;
    
    for (NSInteger i = 0; i<items.count; i++) {
        Post *item = items[i];
        if (!item.lat || !item.lng) {
            continue;
        }
        BKAnnotation *anno = [[BKAnnotation alloc] init];
        anno.coordinate = CLLocationCoordinate2DMake([item.lat doubleValue], [item.lng doubleValue]);
        anno.title = item.addr;
        anno.subtitle = item.content;
        anno.userInfo = item;
        [self.mapView addAnnotation:anno];
        
        
        MKMapPoint p = MKMapPointForCoordinate(anno.coordinate);
        MKMapRect rect = MKMapRectMake(p.x, p.y, 0, 0);
        if ( MKMapRectIsNull(visibleRect)) {
            visibleRect = rect;
            
            //            visibleRegion.center = CLLocationCoordinate2DMake(39.922932f, 116.445100f);
            //            [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(39.922932f, 116.445100f) animated:YES];
            //            [self.mapView setRegion:visibleRegion];
            continue;
        }
        visibleRect = MKMapRectUnion(visibleRect, rect);
    }
    visibleRect = MKMapRectInset(visibleRect, -visibleRect.size.width/3, -visibleRect.size.height/3);
    [self.mapView setVisibleMapRect:visibleRect];
    CGFloat zoomLevel = log2(360.0 * (self.frame.size.width) / 256.0 / self.mapView.region.span.longitudeDelta);
    DLOG(@"zoomLevel:%f", zoomLevel);
    zoomLevel = zoomLevel>15.0f?15.0f:zoomLevel;
    MKCoordinateSpan span = MKCoordinateSpanMake(0, 360/pow(2, zoomLevel)*self.frame.size.width/256);
    [self.mapView setRegion:MKCoordinateRegionMake(self.mapView.centerCoordinate, span) animated:NO];
    
    //    [self.mapView setVisibleMapRect:visibleRect animated:YES];
}
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
    if([annotation isKindOfClass:[BKAnnotation class]]){
        Post *post = ((BKAnnotation*)annotation).userInfo;
        static NSString *ANNO_VIEW_ID = @"BKAnnotationView";
        BKAnnotationView *annotationView = (BKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ANNO_VIEW_ID];
        if (!annotationView) {
            annotationView = [[BKAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:ANNO_VIEW_ID];
            annotationView.opaque = NO;
            [annotationView setDraggable:YES];
            annotationView.canShowCallout = YES;
        }
        PostItem *postItem = post.items.firstObject;
        annotationView.annotation = annotation;
        [annotationView setImageURLString:postItem.url];
        return annotationView;
    }
    return nil;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
    if ([view.annotation isKindOfClass:[BKAnnotation class]]) {
        BKAnnotation *anno = view.annotation;
        Post *post = anno.userInfo;
        if (self.delegate && [self.delegate respondsToSelector:@selector(photoMapView:didPhotoRecordTapped:)]){
            [self.delegate photoMapView:self didPhotoRecordTapped:post];
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
